package com.ywxx.dyhelper.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ywxx.dyhelper.R;
import com.ywxx.dyhelper.model.ResponseModel;
import com.ywxx.dyhelper.utils.ConstAPI;
import com.ywxx.dyhelper.utils.ConstValues;
import com.ywxx.dyhelper.utils.HttpUtils;
import com.ywxx.dyhelper.utils.ParamsHelper;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 授权界面
 */
public class AuthorizationActivity extends AppCompatActivity
{

    TextView labDeviceID;
    EditText tbchannel;
    EditText tbkey;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authorization);

        InitUI();
    }

    private void InitUI()
    {
        setTitle("授权验证");

        labDeviceID = findViewById(R.id.labDeviceID);
        tbchannel = findViewById(R.id.tbchannel);
        tbkey = findViewById(R.id.tbkey);
        labDeviceID = findViewById(R.id.labDeviceID);
        button = findViewById(R.id.btnCheck);

        labDeviceID.setText("设备ID:" + ConstValues.getInstance().getIMEI(this));


        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                UIToData();
            }
        });

    }

    void UIToData()
    {
        if (TextUtils.isEmpty(tbkey.getText()))
        {
            Toast.makeText(this, "请输入授权码", Toast.LENGTH_LONG).show();
        }

        ParamsHelper params = new ParamsHelper(ConstAPI.授权检验);
        params.Add("deviceno", ConstValues.getIMEI(this));
        params.Add("code", tbkey.getText().toString().trim());
        params.Add("channel", tbchannel.getText().toString().trim());

        new HttpUtils(params)
        {
            @Override
            public void onHttpResult(ResponseModel response)
            {
                if (response.status == 1)
                {
                    Toast.makeText(AuthorizationActivity.this, "授权成功,请重新打开应用...", Toast.LENGTH_LONG).show();
                    AuthorizationActivity.this.finish();
                } else
                {
                    Toast.makeText(AuthorizationActivity.this, response.msg, Toast.LENGTH_LONG).show();
                }
            }
        };
    }

}